package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.linkplay.lpvr.avslib.LPAVSManager;
import com.linkplay.lpvr.lpvrcallback.AvsSendEventCallback;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.LanguageBean;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AlexaLanguageActivity extends BaseAppCompatActivity implements AdapterView.OnItemClickListener, AvsSendEventCallback {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.lv_language)
    ListView lvLanguage;
    @BindView(R.id.bt_confirm)
    Button btConfirm;
    @BindView(R.id.pb_confirm)
    ProgressBar pbConfirm;
    private CommonAdapter<LanguageBean> languageBeanCommonAdapter;
    private List<LanguageBean> datas;
    private int postion = 0;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_alexa_language;
    }

    @Override
    protected void initViewsAndEvents() {

    }

    @Override
    protected void initData() {
        datas = new ArrayList<>();
        datas.add(new LanguageBean(R.drawable.alexa_language_img_australia, "English(Australia)", false, LPAVSManager.LpAvsSettingsLocal.LP_AVS_SETTINGS_EN_AU));
        datas.add(new LanguageBean(R.drawable.alexa_language_img_canada, "English(Canada)", false, LPAVSManager.LpAvsSettingsLocal.LP_AVS_SETTINGS_EN_CA));
        datas.add(new LanguageBean(R.drawable.alexa_language_img_uk, "English(UK)", false, LPAVSManager.LpAvsSettingsLocal.LP_AVS_SETTINGS_EN_GB));
        datas.add(new LanguageBean(R.drawable.alexa_language_img_us, "English(US)", false, LPAVSManager.LpAvsSettingsLocal.LP_AVS_SETTINGS_EN_US));
        datas.add(new LanguageBean(R.drawable.alexa_language_img_francais, "Français", false, LPAVSManager.LpAvsSettingsLocal.LP_AVS_SETTINGS_FR_FR));
        datas.add(new LanguageBean(R.drawable.alexa_language_img_deutsch, "Deutsch", false, LPAVSManager.LpAvsSettingsLocal.LP_AVS_SETTINGS_DE_DE));
        int language = preferenceUtil.getLanguage();
        datas.get(language).setSelected(true);
        languageBeanCommonAdapter = new CommonAdapter<LanguageBean>(this, datas, R.layout.item_language) {
            @Override
            public void convert(ViewHolder helper, int position, LanguageBean item) {
                ImageView iv_selecter = helper.getView(R.id.iv_selecter);
                ImageView iv_language = helper.getView(R.id.iv_language);
                TextView tv_language = helper.getView(R.id.tv_language);
                iv_language.setImageResource(item.getId());
                tv_language.setText(item.getLanguage());
                if (item.isSelected()) {
                    iv_selecter.setVisibility(View.VISIBLE);
                } else {
                    iv_selecter.setVisibility(View.GONE);
                }
            }
        };
        lvLanguage.setAdapter(languageBeanCommonAdapter);
        lvLanguage.setOnItemClickListener(this);
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @OnClick({R.id.iv_back, R.id.bt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_confirm:
                btConfirm.setVisibility(View.GONE);
                pbConfirm.setVisibility(View.VISIBLE);
                LPAVSManager.getInstance(this).updateLPAVSLocal(datas.get(postion).getCode(), this);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        postion = position;
        for (int i = 0; i < datas.size(); i++) {
            datas.get(i).setSelected(false);
        }
        datas.get(position).setSelected(true);
        languageBeanCommonAdapter.notifyDataSetChanged();
    }

    @Override
    public void success(int i) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btConfirm.setVisibility(View.VISIBLE);
                pbConfirm.setVisibility(View.GONE);
                preferenceUtil.setLanguage(postion);
                ToastUtils.showLongToast(AlexaLanguageActivity.this, R.string.language_selector_success);

            }
        });

    }
    @Override
    public void failure() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btConfirm.setVisibility(View.VISIBLE);
                pbConfirm.setVisibility(View.GONE);
                ToastUtils.showLongToast(AlexaLanguageActivity.this, R.string.language_selector_faild);

            }
        });

    }
    @Override
    public void complete() {

    }
}
